// pages/home/home.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: "",
    name: "",
    address: "",
    list:[],
    dataIndex:null,
    show:false,
    hidId:null,
    region: [],
    customItem: '请选择'
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  phone: function (e) {
    let name = e.detail.value
    this.setData({
      phone: name
    })
    console.log(this.data.phone);
  },
  name: function (e) {
    this.setData({
      name: e.detail.value
    })
    console.log(this.data.name);
  },
  address: function (e) {
    this.setData({
      address: e.detail.value
    })
    console.log(this.data.address);
  },
  sare(){
    console.log(this.data.name);
   let index= this.data.dataIndex.findIndex((item)=>item.id == this.data.hidId)
   if(index != -1){
     this.data.dataIndex[this.data.hidId].id=this.data.hidId,
     this.data.dataIndex[this.data.hidId].name=this.data.name,
     this.data.dataIndex[this.data.hidId].phone=this.data.phone,
     this.data.dataIndex[this.data.hidId].address=this.data.address,
     console.log(this.data.dataIndex);
   }
  //  重新覆盖掉本地的数据
   wx.setStorageSync('obj', this.data.dataIndex)
   wx.setStorageSync('region', this.data.region)
  //  返回上一级
   wx.navigateBack({delta:1})
  },

  // 保存
  submit() {
    if (this.data.name == "" && this.data.phone == "" && this.data.address == "") {
      wx.showToast({
        title: '请输入内容',
        icon: 'error',
        duration: 2000
      })
      
    } else {
      this.data.list.push({
        "id":this.data.list.length,
        "name":this.data.name,
        "phone":this.data.phone,
        "address":this.data.address,
      })
      console.log(this.data.list);
      wx.setStorageSync('obj',this.data.list )
      wx.setStorageSync('region',this.data.region)
      wx.redirectTo({
        url: '/pages/addre/addre',
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      hidId:options.id
    })
    console.log(this.data.hidId)
    let arry=wx.getStorageSync('obj')
    if(arry){
      this.setData({
        list:arry
      })
    }


    let abs=wx.getStorageSync('obj')
    this.setData({
      dataIndex:abs
    })
    console.log(this.data.dataIndex);
    this.setData({
      name:this.data.dataIndex[options.id].name,
      phone:this.data.dataIndex[options.id].phone,
      address:this.data.dataIndex[options.id].address,
      show:true
      
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})